package com.x.erp.dao;

import com.x.erp.model.IntelligentRobot;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 大屏智能机器人信息DAO
 */
@Repository
public class IntelligentRobotDao extends BaseDao {

	private final static String selectIntelligentRobotListSql = "SELECT eir.* FROM erp_intelligent_robot eir WHERE 1=1 ";
	/**
	 * 查询大屏智能机器人列表
	 * @param intelligentRobot
	 * @return
	 * @throws Exception
	 */
	public List<IntelligentRobot> selectIntelligentRobotList(IntelligentRobot intelligentRobot) throws Exception{
		StringBuilder sql = new StringBuilder(selectIntelligentRobotListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, intelligentRobot);
		sql.append("ORDER BY eir.ROBOT_SEQ ");
		return this.queryForList(sql.toString(), IntelligentRobot.class, params, intelligentRobot);
	}

	private final static String selectIntelligentRobotNumSql = "SELECT COUNT(eir.ROBOT_ID) FROM erp_intelligent_robot eir WHERE 1=1 ";
	/**
	 * 查询大屏智能机器人数量
	 * @param intelligentRobot
	 * @return
	 * @throws Exception
	 */
	public int selectIntelligentRobotNum(IntelligentRobot intelligentRobot) throws Exception{
		StringBuilder sql = new StringBuilder(selectIntelligentRobotNumSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, intelligentRobot);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param intelligentRobot
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, IntelligentRobot intelligentRobot) {
		if(intelligentRobot != null) {
			if(StringUtils.isNotEmpty(intelligentRobot.getRobotId())){
				sql.append("AND eir.ROBOT_ID = ? ");
				params.add(intelligentRobot.getRobotId());
			}
			if(intelligentRobot.getRobotState() != null){
				sql.append("AND eir.ROBOT_STATE = ? ");
				params.add(intelligentRobot.getRobotState());
			}
			if(intelligentRobot.getRobotCreateTimeFrom() != null){
				sql.append("AND eir.ROBOT_CREATE_TIME >= ? ");
				params.add(intelligentRobot.getRobotCreateTimeFrom());
			}
			if(intelligentRobot.getRobotCreateTimeTo() != null){
				sql.append("AND eir.ROBOT_CREATE_TIME < ? ");
				params.add(this.getDate(intelligentRobot.getRobotCreateTimeTo(), 1));
			}
		}
	}

	private final static String selectIntelligentRobotSql = "SELECT " +
			"eir.*, " +
			"est.TEMPLATE_CITY_ID, " +
			"est.TEMPLATE_CITY_NAME, " +
			"est.TEMPLATE_BRAND_ID, " +
			"est.TEMPLATE_BRAND_NAME, " +
			"est.TEMPLATE_SERIES_ID, " +
			"est.TEMPLATE_SERIES_NAME, " +
			"est.TEMPLATE_PRICE_FROM, " +
			"est.TEMPLATE_PRICE_TO, " +
			"est.TEMPLATE_CAR_AGE_FROM, " +
			"est.TEMPLATE_CAR_AGE_TO, " +
			"est.TEMPLATE_MILEAGE_FROM, " +
			"est.TEMPLATE_MILEAGE_TO, " +
			"est.TEMPLATE_GEARBOX, " +
			"est.TEMPLATE_OUT_COLOR, " +
			"est.TEMPLATE_CAR_TYPE_ID, " +
			"est.TEMPLATE_CAR_TYPE_NAME, " +
			"est.TEMPLATE_ENVIRON, " +
			"est.TEMPLATE_CONFIG_ID, " +
			"est.TEMPLATE_CONFIG_NAME " +
			"FROM " +
			"erp_intelligent_robot eir " +
			"LEFT JOIN erp_select_template est ON eir.TEMPLATE_ID = est.TEMPLATE_ID " +
			"WHERE " +
			"eir.ROBOT_ID = ? ";
	/**
	 * 查询大屏智能机器人信息
	 * @param intelligentRobot
	 * @return
	 */
	public IntelligentRobot selectIntelligentRobot(IntelligentRobot intelligentRobot) throws Exception {
		StringBuilder sql = new StringBuilder(selectIntelligentRobotSql);
		List<Object> params = new ArrayList<>();
		params.add(intelligentRobot.getRobotId());
		searchCondition(sql, params, intelligentRobot);
		List<IntelligentRobot> intelligentRobots = this.queryForList(sql.toString(), IntelligentRobot.class, params);
		if(intelligentRobots.isEmpty()){
			return null;
		}else{
			return intelligentRobots.get(0);
		}
	}

}
